package http

import (
	"errors"

	"gitee.com/lcc_dr/restful-api-demo/apps/host"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"github.com/infraboard/mcube/http/response"
)

// http gin handle createHost具体实现方法
func (h *HandleHostService) createHost(c *gin.Context) {

	createHostReq := host.Newhost()
	// 用户传递过来的参数进行解析
	// If `GET`, only `Form` binding engine (`query`) used.
	// If `POST`, first checks the `content-type` for `JSON` or `XML`, then uses `Form` (`form-data`).
	if err := c.Bind(createHostReq); err != nil {
		response.Failed(c.Writer, err)
		return
	}

	validate = validator.New()
	err := validate.Struct(createHostReq)
	if err != nil {
		response.Failed(c.Writer, err)
		return
	}

	// 进行接口调用
	ins, err := h.svc.CreateHost(c.Request.Context(), createHostReq)
	if err != nil {
		response.Failed(c.Writer, err)
		return
	}

	response.Success(c.Writer, ins)
}

// http gin handle queryHost
func (h *HandleHostService) queryDescribeHost(c *gin.Context) {

	req := host.NewDescribeHostRequest()

	// 用户传递过来的参数进行解析
	if err := c.ShouldBindUri(req); err != nil {
		response.Failed(c.Writer, err)
		return
	}
	//参数校验
	validate = validator.New()
	err := validate.Struct(req)
	if err != nil {
		response.Failed(c.Writer, err)
		return
	}

	// 进行接口调用
	hosts, err := h.svc.DescribeHost(c.Request.Context(), req)

	if err != nil {

		err = errors.New("当前id不存在")
		response.Failed(c.Writer, err)
		return
	}

	response.Success(c.Writer, hosts)
}

// htpp gin handle queryHost
func (h *HandleHostService) queryHosts(c *gin.Context) {

	req := host.NewQueryHostsRequest()
	c.Bind(req)
	validate = validator.New()
	err := validate.Struct(req)
	if err != nil {
		response.Failed(c.Writer, err)
		return
	}

	// 进行接口调用
	hosts, err := h.svc.QueryHosts(c.Request.Context(), req)
	if err != nil {
		response.Failed(c.Writer, err)
		return
	}

	response.Success(c.Writer, hosts)
}

// http gin handle deleteHost
func (h *HandleHostService) deleteHost(c *gin.Context) {
	deleteReq := host.NewDeleteHostRequest()

	// 用户传递过来的参数进行解析
	if err := c.ShouldBindUri(deleteReq); err != nil {
		response.Failed(c.Writer, err)
		return
	}

	//参数校验
	validate = validator.New()
	err := validate.Struct(deleteReq)
	if err != nil {
		response.Failed(c.Writer, err)
		return
	}

	queryReq := host.NewDescribeHostRequest()
	queryReq.ID = deleteReq.ID

	//1、删除前先查询下
	//2、不存在即返回
	//3、存在执行删除
	//4、删除完再查询确认已删除

	// 进行接口调用
	queryHost, err := h.svc.DescribeHost(c.Request.Context(), queryReq)
	if err != nil {
		//当前id 不存在
		response.Failed(c.Writer, err)
		return
	}
	_, err = h.svc.DeleteHost(c.Request.Context(), deleteReq)
	if err != nil {
		response.Failed(c.Writer, err)
		return
	}

	response.Success(c.Writer, queryHost)
}

// http gin handle patchHost
func (h *HandleHostService) updatePatchHost(c *gin.Context) {

	//1、更新前先查询下
	//2、不存在即返回
	//3、存在执行
	//4、删除完再查询确认已删除

	//获取id
	queryReqId := host.NewDescribeHostRequest()
	if err := c.ShouldBindUri(queryReqId); err != nil {
		response.Failed(c.Writer, err)
		return
	}
	//id参数校验
	validate = validator.New()
	err := validate.Struct(queryReqId)
	if err != nil {
		response.Failed(c.Writer, err)
		return
	}
	//查询id是否存在
	_, err = h.svc.DescribeHost(c.Request.Context(), queryReqId)
	if err != nil {
		//当前id 不存在
		response.Failed(c.Writer, errors.New("host not found"))
		return
	}

	//获取update 参数
	updatePatchHostRequest := host.NewUpdatePatchHostRequest()
	// 用户传递过来的参数进行解析
	if err := c.Bind(updatePatchHostRequest); err != nil {
		response.Failed(c.Writer, err)
		return
	}
	if queryReqId.ID != updatePatchHostRequest.ID {
		response.Failed(c.Writer, errors.New("body id no Equal uri id"))
		return
	}

	// 进行接口调用
	hosts, err := h.svc.UpdatePatchHost(c.Request.Context(), updatePatchHostRequest)

	if err != nil {
		response.Failed(c.Writer, err)
		return
	}

	response.Success(c.Writer, hosts)

}

func (h *HandleHostService) updatePutHost(c *gin.Context) {

	//1、更新前先查询下
	//2、不存在即返回
	//3、存在执行
	//4、删除完再查询确认已删除

	//获取id
	queryReqId := host.NewDescribeHostRequest()
	if err := c.ShouldBindUri(queryReqId); err != nil {
		response.Failed(c.Writer, err)
		return
	}
	//id参数校验
	validate = validator.New()
	err := validate.Struct(queryReqId)
	if err != nil {
		response.Failed(c.Writer, err)
		return
	}
	//查询id是否存在
	_, err = h.svc.DescribeHost(c.Request.Context(), queryReqId)
	if err != nil {
		//当前id 不存在
		response.Failed(c.Writer, errors.New("host not found"))
		return
	}

	//获取update 参数
	updatePutHostRequest := host.NewUpdatePutHostRequest()
	// 用户传递过来的参数进行解析
	if err := c.Bind(updatePutHostRequest); err != nil {
		response.Failed(c.Writer, err)
		return
	}
	if queryReqId.ID != updatePutHostRequest.Host.Id {
		response.Failed(c.Writer, errors.New("body id no Equal uri id"))
		return
	}

	hosts, err := h.svc.UpdatePutHost(c.Request.Context(), updatePutHostRequest)

	if err != nil {
		response.Failed(c.Writer, err)
		return
	}

	response.Success(c.Writer, hosts)

}
